package com.snowfield.lb47.lbconfig.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteList;
import com.snowfield.lb47.lbconfig.domain.HttpCheckMethod;
import com.snowfield.lb47.lbconfig.mapper.HttpCheckMethodMapper;
import com.snowfield.lb47.lbconfig.service.IHttpCheckMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * HTTP检测方法Service业务层处理
 *
 * @author snowfield
 * @date 2024-05-28
 */
@Service
public class HttpCheckMethodServiceImpl implements IHttpCheckMethodService {
    @Autowired
    private HttpCheckMethodMapper httpCheckMethodMapper;

    /**
     * 查询HTTP检测方法
     *
     * @param id HTTP检测方法主键
     * @return HTTP检测方法
     */
    @Override
    public HttpCheckMethod selectHttpCheckMethodById(Long id) {
        return httpCheckMethodMapper.selectHttpCheckMethodById(id);
    }

    /**
     * 查询HTTP检测方法列表
     *
     * @param httpCheckMethod HTTP检测方法
     * @return HTTP检测方法
     */
    @Override
    public List<HttpCheckMethod> selectHttpCheckMethodList(HttpCheckMethod httpCheckMethod) {
        return httpCheckMethodMapper.selectHttpCheckMethodList(httpCheckMethod);
    }

    /**
     * 新增HTTP检测方法
     *
     * @param httpCheckMethod HTTP检测方法
     * @return 结果
     */
    @Override
    public int insertHttpCheckMethod(HttpCheckMethod httpCheckMethod) {
        httpCheckMethod.setCreateTime(DateUtils.getNowDate());
        //非https时，sni不配置
        if (httpCheckMethod.getHttps().equals("N")){
            httpCheckMethod.setSni("");
        }
        if (httpCheckMethod.getExpectType().equals("responseCode")){
            //配置响应码时，响应内容不配置
            httpCheckMethod.setExpectContent("");
        } else if (httpCheckMethod.getExpectType().equals("responseContent")){
            //配置响应内容时，响应码不配置
            httpCheckMethod.setExpectStatus("");
        }

        return httpCheckMethodMapper.insertHttpCheckMethod(httpCheckMethod);
    }

    /**
     * 修改HTTP检测方法
     *
     * @param httpCheckMethod HTTP检测方法
     * @return 结果
     */
    @Override
    public int updateHttpCheckMethod(HttpCheckMethod httpCheckMethod) {
        httpCheckMethod.setUpdateTime(DateUtils.getNowDate());

        if (httpCheckMethod.getHttps().equals("N")){
            httpCheckMethod.setSni("");
        }
        if (httpCheckMethod.getExpectType().equals("responseCode")){
            httpCheckMethod.setExpectContent("");
        } else if (httpCheckMethod.getExpectType().equals("responseContent")){
            httpCheckMethod.setExpectStatus("");
        }
        return httpCheckMethodMapper.updateHttpCheckMethod(httpCheckMethod);
    }

    /**
     * 批量删除HTTP检测方法
     *
     * @param ids 需要删除的HTTP检测方法主键
     * @return 结果
     */
    @Override
    public int deleteHttpCheckMethodByIds(Long[] ids) {
        return httpCheckMethodMapper.deleteHttpCheckMethodByIds(ids);
    }

    /**
     * 删除HTTP检测方法信息
     *
     * @param id HTTP检测方法主键
     * @return 结果
     */
    @Override
    public int deleteHttpCheckMethodById(Long id) {
        return httpCheckMethodMapper.deleteHttpCheckMethodById(id);
    }

    /**
     * 校验HTTP检测方法名称是否唯一
     *
     * @param httpCheckMethod HTTP检测方法
     * @return 结果
     */
    @Override
    public Boolean checkNameUnique(HttpCheckMethod httpCheckMethod){
        Long methodId = httpCheckMethod.getId();
        HttpCheckMethod method = httpCheckMethodMapper.selectHttpCheckMethodByName(httpCheckMethod.getMethodName());
        if (method != null && !method.getId().equals(methodId)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
